*-------------------------------------------------------------------------------------------------------*
*RESEARCHERS:		Mette Foged, Linea Hasager and Vasil Yasenov
*PROJECT:			Meta-analysis
*DESCRIPTION:		1  Load database
*					2  Generate additional variables
*					3  Run regressions and output
*-------------------------------------------------------------------------------------------------------*
*ssc install estout, replace
*ssc install unique, replace

clear all
set more off


global ctr			eu northamerica restworld
global x_ctr		eu restworld
global x_macro	 	Unemployment GDPgrowth
global x_study 		i.method i.data_frequency i.skill

use "${data}\database_clean.dta", replace


*-------------------------------------------------------------------------------------------------------*
*-------------------------------------------------------------------------------------------------------*

* Rest of the world
gen restworld=0
replace restworld=1 if eu==0 & northamerica==0

*Nordic countries
gen nordic=0
replace nordic=1 if country == "Norway" | country == "Sweden" |country == "Denmark" |country == "Finland"
	
*Rest of Europe
gen resteu=0
replace resteu=1  if country=="Austria" | country=="France" | country=="Germany" | country=="Greece" ///
| country=="Ireland" | country=="Italy" | country=="Netherlands" | country=="Portugal" | country=="Spain" ///
| country=="Switzerland" | country=="United Kingdom" | country=="Western Europe" | country=="European Economic Area" | country=="Albania"



tab region*
replace region="Rest of Europe" if resteu==1
replace region="Nordic Countries" if nordic==1
replace region="Rest of World" if region=="Rest of the world"

*region, collapsing nordic countries and rest of europe into one
gen		region2="Europe" if eu==1
replace region2="North America" if northamerica==1
replace region2="Rest of the World" if region=="Rest of World"
tab region2


replace wage = 0 if wage == .
tab wage empl, m

replace skill="All" if skill=="all"
replace skill="High Skill" if skill=="High"
replace skill="Low Skill" if skill=="Low"

replace data_frequency="Short Run" 	if data_frequency=="Annual or more frequent"
replace data_frequency="Long Run" 	if data_frequency=="Less frequent"

gen yearfe = int(firstyear/10) * 10
gen pubyearfe = int(year/10)*10


gen top100="Not top 100"
replace top100="Top 100" if journal_ranking < 101

gen top50="Outside Top 50"
replace top50="Top 50" if journal_ranking < 51

replace method="Simulation" 					if method=="simulation"
replace method="Difference-in-Differences"		if method=="DID"
replace method="Difference-in-Differences IV"	if method=="DIDIV"
replace method="First-Difference"				if method=="FD"
replace method="First-Difference IV"			if method=="FDIV"

gen 	method2="IV" if inlist(method,"IV","First-Difference IV","Difference-in-Differences IV")
replace	method2="OLS" if method=="OLS"
replace	method2="Diff-in-Diff, First-Difference" if inlist(method,"Difference-in-Differences","First-Difference")


gen avg=1 if variation=="Area"
replace avg=0 if variation=="Skill" | variation=="Mixture"

gen refugee="Refugees" if refugees==1
replace refugee="Workers, mixture or other" if refugees==0

summ ratio_f_n, d
gen relativelowskill="Relatively Low Skilled" if ratio_f_n !=.
replace relativelowskill="Relatively High Skilled" if ratio_f_n < 1 & ratio_f_n !=.
replace relativelowskill="Unknown" if ratio_f_n==.


*Create categories for IV
gen		 	IV_type="IV (Bartik Type)" if IV=="bartik_type"
replace 	IV_type="IV (Other)" if (IV=="other" | IV=="lagged_inflow") 
replace 	IV_type="OLS" if IV=="" & naturalexperiment!=1
replace		IV_type="Natural Experiment" if naturalexperiment==1 & IV==""
replace		IV_type="IV (Natural Experiment)" if naturalexperiment==1 & IV!=""
ta IV_type



* Weights
gen p_se=1/se
gen p_score=1/Score

* Distinguish relative versus average effects
tab variation
gen relative=0
replace relative=1 if variation=="Skill" | variation=="Mixture" 
replace relative=. if variation !="Skill" & variation!="Mixture" & variation !="Area"
gen area=0
replace area=1 if variation=="Area"

*-------------------------------------------------------------------------------------------------------*
* TABLE : REPLICATION OF PREVIOUS WORK AND MAIN STATISTICS
*-------------------------------------------------------------------------------------------------------*

*Split outcome in wages and employment
gen 	outcome_empl="Employment Rate" 				if outcome=="e01"
replace outcome_empl="Unemployment Rate" 			if outcome=="u01"

gen 	outcome_empl2=outcome_empl

gen 	outcome_wage="Hourly Wage" 					if outcome=="w"
replace	outcome_wage="Daily Wage"		 			if outcome=="wxday"
replace	outcome_wage="Monthly Wage"		 			if outcome=="wxmonth"
replace	outcome_wage="Quarterly Wage"	 			if outcome=="wxquarter"
replace	outcome_wage="Unclear Wage Variable"		if outcome=="wxunclear"
replace	outcome_wage="Weekly Wage"		 			if outcome=="wxweek"
replace	outcome_wage="Yearly Wage"		 			if outcome=="wxyear"

gen 	outcome_wage2="Hourly, Daily, Weekly Wage"	 	if inlist(outcome_wage,"Hourly Wage","Daily Wage","Weekly Wage")
replace	outcome_wage2="Monthly, Quarterly, Yearly Wage"	if inlist(outcome_wage,"Monthly Wage","Quarterly Wage","Yearly Wage")
replace	outcome_wage2="Unclear Wage Variable"			if inlist(outcome_wage,"Unclear Wage Variable")


ta method IV
replace IV="Instrument: Bartik" 			if IV=="bartik_type"
replace IV="Instrument: Lagged Inflow" 		if IV=="lagged_inflow"
replace IV="Instrument: Other"	 if IV=="other" 
replace IV="No Instrument"		if missing(IV)
ta IV

gen		IV2="Instrument: Bartik, Lagged Inflow" 	if inlist(IV,"Instrument: Bartik","Instrument: Lagged Inflow")
replace IV2="Instrument: Other"	 					if inlist(IV,"Instrument: Other") 
replace IV2="No Instrument"							if inlist(IV,"No Instrument")
ta IV2

ta naturalexperiment
gen naturalexperiment_string="No Natural Experiment" if missing(naturalexperiment)
replace naturalexperiment_string="Natural Experiment" if naturalexperiment==1
replace naturalexperiment=0 if missing(naturalexperiment)
drop naturalexperiment
rename naturalexperiment_string naturalexperiment





*COMBINED TABLE OF WAGES AND EMPLOYMENT

*ALL EFFECTS
foreach type in wage empl {
local wage_txt "Wage Effects"
local empl_txt "Employment Effects"

*Average vs. total wages
forvalues i = 0/1 {
preserve
keep if `type'==1

*Total number of papers/estimates in database for avg wages
keep if avg==`i'
gen n=_N

bysort paper_id: gen n_papers =_n==1
replace n_papers=sum(n_papers)
replace n_papers=n_papers[_N]


*Weight studies equally
bysort paper_id: gen p_N = 1/_N
collapse (mean) mean_avg`i'=estimate n_papers_avg`i'=n_papers n_avg`i'=n (median) median_avg`i'=estimate /*[weight=p_N]*/

gen m="All"
 save "${data}\tot_avg`i'.dta", replace
restore
}

preserve
keep if `type'==1
*Total number of papers/estimates in database
gen n=_N
bysort paper_id: gen n_papers =_n==1
replace n_papers=sum(n_papers)
replace n_papers=n_papers[_N]


*Weight studies equally
bysort paper_id: gen p_N = 1/_N
bysort paper_id avg: gen p_N_avg=1/_N
 collapse (mean) mean=estimate n_papers=n_papers n=n (median) median=estimate (sd) sd=estimate /*[weight=p_N]*/

gen m="All"

merge 1:1 m using "${data}\tot_avg1.dta"
drop _merge
merge 1:1 m using "${data}\tot_avg0.dta"
drop _merge


format median mean sd mean_avg0 mean_avg1 median_avg0 median_avg1 %9.2fc
format n n_papers n_avg0 n_papers_avg0 n_avg1 n_papers_avg1 %9.0fc

*rename
foreach var in median mean sd mean_avg0 mean_avg1 median_avg0 median_avg1 n n_papers n_avg0 n_papers_avg0 n_avg1 n_papers_avg1 {
rename `var' `var'_`type'
}

 save "${data}\tot_avg_comb_`type'.dta", replace

restore
}
 
 
 preserve
 clear all
 use  "${data}\tot_avg_comb_wage.dta"
merge 1:1 m using "${data}\tot_avg_comb_empl.dta"
drop _merge

listtex m mean_avg0_wage median_avg0_wage n_avg0_wage n_papers_avg0_wage mean_avg1_wage median_avg1_wage n_avg1_wage n_papers_avg1_wage mean_avg0_empl median_avg0_empl n_avg0_empl n_papers_avg0_empl mean_avg1_empl median_avg1_empl n_avg1_empl n_papers_avg1_empl ///
using "${out}/table1_total_combined.tex",  ///
rstyle(tabular) replace ///
head(\begin{tabular}{l*{20}r} \toprule  & \multicolumn{4}{c}{Panel A: Wage, Relative} & \multicolumn{4}{c}{Panel B: Wage, Total} & \multicolumn{4}{c}{Panel C: Employment, Relative} & \multicolumn{4}{c}{Panel D: Employment, Total} \\  \cmidrule(lr){2-5} \cmidrule(lr){6-9}  \cmidrule(lr){10-13} \cmidrule(lr){14-17} & Mean & Median & N & Studies & Mean & Median & N & Studies & Mean & Median & N & Studies & Mean & Median & N & Studies \\ \midrule) 


erase "${data}\tot_avg0.dta"
erase "${data}\tot_avg1.dta"
erase "${data}\tot_avg_comb_wage.dta"
erase "${data}\tot_avg_comb_empl.dta"
restore



*BY SUBGROUPS
foreach var of varlist region2 skill top50 IV_type {

*Average vs. total wages
forvalues i = 0/1 {

foreach type in wage empl {
local wage_txt "Wage Effects"
local empl_txt "Employment Effects"

preserve
keep if `type'==1

*Total number of papers/estimates in database for avg wages
keep if avg==`i' & `var'!="."
bysort `var': gen n=_N

bysort paper_id `var': gen n_papers =_n==1
bysort `var': replace n_papers=sum(n_papers)
bysort `var': replace n_papers=n_papers[_N]


*Weight studies equally within regions
bysort paper_id `var': gen p_N = 1/_N
collapse (mean) mean_avg`i'_`type'=estimate n_papers_avg`i'_`type'=n_papers n_avg`i'_`type'=n (median) median_avg`i'_`type'=estimate /*[weight=p_N]*/, by(`var')

save "$data\`var'_avg`i'_`type'.dta", replace
restore
}
}

preserve
clear all
use  "$data\`var'_avg0_wage.dta"
merge 1:1 `var' using "$data\`var'_avg1_wage.dta", nogen
merge 1:1 `var' using "$data\`var'_avg0_empl.dta", nogen
merge 1:1 `var' using "$data\`var'_avg1_empl.dta", nogen



save  "$data\`var'_combined.dta", replace

format mean_avg0_wage mean_avg1_wage median_avg0_wage median_avg1_wage mean_avg0_empl mean_avg1_empl median_avg0_empl median_avg1_empl %9.2fc
format n_avg0_wage n_papers_avg0_wage n_avg1_wage n_papers_avg1_wage n_avg0_empl n_papers_avg0_empl n_avg1_empl n_papers_avg1_empl %9.0fc

local region2_txt 			"By Region"
local skill_txt 			"By Native Education Group"
local top50_txt 			"By Journal Rank"
local IV_type_txt 			"By Empirical Strategy"



listtex `var'  mean_avg0_wage median_avg0_wage n_avg0_wage n_papers_avg0_wage mean_avg1_wage median_avg1_wage n_avg1_wage n_papers_avg1_wage mean_avg0_empl median_avg0_empl n_avg0_empl n_papers_avg0_empl mean_avg1_empl median_avg1_empl n_avg1_empl n_papers_avg1_empl ///
using "${out}/table1_`var'_combined.tex",  ///
rstyle(tabular) replace ///
head(  & \multicolumn{16}{c}{\emph{``var'_txt'}} \\) 


restore
erase "$data\`var'_avg0_empl.dta"
erase "$data\`var'_avg1_empl.dta"
erase "$data\`var'_avg0_wage.dta"
erase "$data\`var'_avg1_wage.dta"
}



*-------------------------------------------------------------------------------------------------------*
* TABLE : MAIN RESULTS
*-------------------------------------------------------------------------------------------------------*

gen cluster=paper_id

local wage_txt "Wage Effects"
local empl_txt "Employment Effects"




foreach type in wage empl {
preserve
keep if `type'==1

*Dummy for dropping OLS if there are IV estimates within study

gen iv=0
replace iv=1 if method=="IV"
gen ols=0
replace ols=1 if method=="OLS"
bysort paper_id: egen _iv=sum(iv)

gen _keep=1
replace _keep=0 if ols==1 &  _iv>0
drop _iv ols iv



*generate numeric variables that can be used as factors

tab method
gen method_d=0
replace method_d=1 if inlist(method,"IV","Difference-in-Differences IV","First-Difference IV")
drop method
rename method_d method


*Factor variables for outcome types
gen		outcome_t=1
replace	outcome_t=0 if outcome_wage2=="Hourly, Daily, Weekly Wage" | outcome_empl=="Employment Rate"
replace outcome_t=2 if  outcome_empl=="Unemployment Rate"


tab data_frequency
gen data_t=0
replace data_t=1 if data_frequency=="Short Run"
drop data_frequency
rename data_t data_frequency

tab skill
gen skill_t=0
replace skill_t=1 if skill=="All"
replace skill_t=2 if skill=="Low Skill"
replace skill_t=3 if skill=="High Skill"
drop skill
rename skill_t skill

tab relativelowskill
gen skill_t=0
replace skill_t=1 if relativelowskill=="Relatively Low Skilled"
replace skill_t=2 if relativelowskill=="Relatively High Skilled"
replace skill_t=3 if relativelowskill=="Unknown"
drop relativelowskill
rename skill_t relativelowskill




label variable StrictEmpInd_avg "\emph{EPL (Regular)}"
label variable CollBarg_avg "\emph{Collective Bargaining}"
label variable AverageTenure_avg "\emph{Average Job Tenure}"
label variable ALMPExp_avg "\emph{ALMP}"
label variable StrictEmpTemp_avg "\emph{EPL (Temporary)}"
label variable StrictEmpColl_avg "\emph{EPL (Collective)}"
label variable PMR_avg "\emph{Product Market Regulation}"
label variable NetReplacementRate_avg "\emph{Net Replacement Rate}"
label variable MintoMedianWage_avg "\emph{Minimum/Median Wage}"



local inst StrictEmpInd_avg
gen inst=1 if `inst'!=.
bysort paper_id `type' relative inst: gen p_N = 1/_N 
drop inst
gen inst=1 if `inst'!=. & se!=.
bysort paper_id `type' relative inst: gen p_N_3 = 1/_N 
eststo clear

eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1,  vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_N], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_score], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_se], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1,  vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_N], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_score], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)

drop inst p_N p_N_3

esttab using ${out}/table3_`type'.tex, /// 
	keep(`inst') /// 
	replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(0)) label noobs ///
	nonum nonotes nogaps postfoot("") posthead("\midrule") nolines prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{5}{c}|ccccc}\toprule") mgroups("Panel A: Relative" "Panel B: Total", pattern(1 0 0 0 0 1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("Unweighted" "Studies" "Impact" "Precision" "Combined" "Unweighted" "Studies" "Impact" "Precision" "Combined") 


**********
**********

foreach inst in StrictEmpColl_avg AverageTenure_avg CollBarg_avg NetReplacementRate_avg {
eststo clear
gen inst=1 if `inst'!=.
bysort paper_id `type' relative inst: gen p_N = 1/_N 
drop inst
gen inst=1 if `inst'!=. & se!=.
bysort paper_id `type' relative inst: gen p_N_3 = 1/_N 
eststo clear
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1,  vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_N], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_score], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_se], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1,  vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_N], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_score], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
drop inst p_N p_N_3

esttab using ${out}/table3_`type'.tex, ///
	keep(`inst') ///
	append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(0)) label noobs ///
	nonum nonotes nogaps postfoot("") prehead("") nolines mtitles("" "" "" "" "" "" "" "" "" "")
}

	
**********
**********


*USING AVG. STANDARDIZED INDEX
local inst index
eststo clear
gen inst=1 if `inst'!=.
bysort paper_id `type' relative inst: gen p_N = 1/_N 
drop inst
gen inst=1 if `inst'!=. & se!=.
bysort paper_id `type' relative inst: gen p_N_3 = 1/_N 

eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1,  vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_N], vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_score], vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_se], vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1,  vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_N], vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_score], vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se], vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)

esttab using ${out}/table3_`type'.tex, ///
	keep(`inst') ///
	scalars("n N" "regionfe \midrule Region FE" "macro Country-Level Controls" "study Study Characteristics") append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) label noobs	nonum nonotes nogaps prehead("") posthead("\hline \\") nolines postfoot("\bottomrule \end{tabular}}") mtitles("" "" "" "" "" "" "" "" "" "")	
	restore
}


